--[[
    Author          ZhengFeng
    Time            2020.12.07
    Description     宠物主界面-查看谱系信息
]]--

require "Common/define"
require "Logic/PetManager"
local Page_PetMainFamilyTree = UIBase.New(PanelResNames.Page_PetMainFamilyTree)
local this = Page_PetMainFamilyTree
local GenerationType =
    {
        Self = 0,           --一代
        Children = 1,       --二代
        Grandchildren = 2,  --三代
        NoGrandpa = 3,      --无外公外婆
        NoGrandFather = 4,  --无爷爷奶奶
    }
local currentPetInfo= nil
local selectedFamilyMember = PetManager.FamilyTree.MySelf
local partListIndex = 0
local partListSelected = 1

function this:Init()
    UIBase.Init(this)
end

function this:OnCreate()
    self.Btn_BGClose = self:GetChild("Btn_BGClose")
    self.Btn_Back = self:GetChild("Btn_Back")
    self:RegisterOnClick(self.Btn_BGClose,self.onClickClose)
    self:RegisterOnClick(self.Btn_Back,self.onClickClose)
    --self.Btn_Back = self:GetChild("Btn_Back")
    --self:RegisterOnClick(self.Btn_Back,self.onClickClose)
    self.Txt_PanelTitle = self:GetChild("Txt_PanelTitle")
    self.List_Part = {}
    for i = 1, 13 do
        local idx = i<10 and "0".. i or i ;
        local item = self:GetChild("Btn_MyPetBody"..idx);
        table.insert(self.List_Part,item)

    end
    --self.List_Part.itemRenderer = this.rendererPartListItem


    --宠物信息
    self.Txt_PetName = self:GetChild("Txt_PetName")
    self.Model_Pet = self:GetChild("Model_Pet")
    self.Txt_OwnerName = self:GetChild("Txt_OwnerName")

    --谱系
    self.GenerationController = self:GetController("GenerationType")
    self.Btn_MySelf = self:GetChild("Btn_MySelf")
    self:RegisterOnClick(self.Btn_MySelf,self.onClickChild)
    self.Txt_MyPetName = self.Btn_MySelf:GetChild("title")
    self.Txt_MyPetName = ""
    self.Img_MyPetIcon = self.Btn_MySelf:GetChild("Model_Pet")

    self.Btn_Father = self:GetChild("Btn_Father")
    self:RegisterOnClick(self.Btn_Father,self.onClickFather)
    self.Txt_FatherName = self.Btn_Father:GetChild("title")
    self.Txt_FatherName = ""
    self.Img_FatherIcon = self.Btn_Father:GetChild("Model_Pet")
    self.Btn_Mother = self:GetChild("Btn_Mother")
    self:RegisterOnClick(self.Btn_Mother,self.onClickMother)
    self.Txt_MotherName = self.Btn_Mother:GetChild("title")
    self.Img_MotherIcon = self.Btn_Mother:GetChild("Model_Pet")

    self.Btn_Grandfather = self:GetChild("Btn_Grandfather")
    self:RegisterOnClick(self.Btn_Grandfather,self.onClickGrandfather)
    self.Txt_GrandfatherName = self.Btn_Grandfather:GetChild("title")
    self.Img_GrandfatherIcon = self.Btn_Grandfather:GetChild("Model_Pet")
    self.Btn_Grandmother = self:GetChild("Btn_Grandmother")
    self:RegisterOnClick(self.Btn_Grandmother,self.onClickGrandmother)
    self.Txt_GrandmotherName = self.Btn_Grandmother:GetChild("title")
    self.Img_GrandmotherIcon = self.Btn_Grandmother:GetChild("Model_Pet")

    self.Btn_Grandpa = self:GetChild("Btn_Grandpa")
    self:RegisterOnClick(self.Btn_Grandpa,self.onClickGrandpa)
    self.Txt_GrandpaName = self.Btn_Grandpa:GetChild("title")
    self.Img_GrandpaIcon = self.Btn_Grandpa:GetChild("Model_Pet")
    self.Btn_Grandma = self:GetChild("Btn_Grandma")
    self:RegisterOnClick(self.Btn_Grandma,self.onClickGrandma)
    self.Txt_GrandmaName = self.Btn_Grandma:GetChild("title")
    self.Img_GrandmaIcon = self.Btn_Grandma:GetChild("Model_Pet")
    this:initModelTable()
end

function this:OnEnable()
    this:showPage()
end

function this:OnDisable()
    currentPetInfo = nil
    this:removePetModel()
    this:removeModelTable()
end
function this:OnDestroy()
    this:removePetModel()
end

function this:onClickClose()
    this:removePetModel()
    closeUI(PanelResNames.Page_PetMainFamilyTree)
end


function this:showPage()
    currentPetInfo = PetManager.GetPetInfoByPetId(PetManager.CurrentPetId)
    --"%s的谱系"
    self.Txt_PanelTitle.text = string.format(getLanguage("PetMain_FamilyTreeInfo"),currentPetInfo.petName)

    this:showGeneticMap()

    this:refreshPetInfo(PetManager.FamilyTree.MySelf)
end

function this:refreshPetInfo(familyMember)
    selectedFamilyMember = familyMember
    this:checkButtonSelected()
    this:showPetInfo()
    this:showPartList()
end
function this:showGeneticMap()
    --self.Txt_MyPetName.text = currentPetInfo.petName
    --self.Img_MyPetIcon.url = currentPetInfo.petIcon
    local _petMutationLevel = PetManager.GetMutationLevel(currentPetInfo.genes)
    self.Btn_MySelf:GetController("level"):SetSelectedIndex(_petMutationLevel-1)
    self.Btn_MySelf:GetChild("frame").url = PetManager.GetMutationLevelFrameIcon(_petMutationLevel)
    --显示自己宠物模型
    local _petListIndex = PetManager.FamilyTree.MySelf + 1
    local _parentGO = self.Img_MyPetIcon
    local _petId = currentPetInfo.petId
    local _genes = currentPetInfo.genes
    this.showFamilyTreePetModel(_petListIndex,_parentGO,_petId,_genes)

    if table.getn(currentPetInfo.geneticMap) == 0 then
        self.GenerationController:SetSelectedIndex(GenerationType.Self)
        return
    end

    --父亲
    local _fatherInfo = currentPetInfo.geneticMap[PetManager.FamilyTree.Father]
    local _hasFather = table.getn(_fatherInfo.genes) > 0
    if _hasFather then
        --self.Txt_FatherName.text = _fatherInfo.petName
        --self.Img_FatherIcon.url = _fatherInfo.petIcon
        local _petMutationLevel = PetManager.GetMutationLevel(_fatherInfo.genes)
        self.Btn_Father:GetController("level"):SetSelectedIndex(_petMutationLevel-1)
        self.Btn_Father:GetChild("frame").url = PetManager.GetMutationLevelFrameIcon(_petMutationLevel)
        --显示父亲宠物模型
        _petListIndex = PetManager.FamilyTree.Father + 1
        _parentGO = self.Img_FatherIcon
        _petId = _fatherInfo.petId
        _genes = _fatherInfo.genes
        this.showFamilyTreePetModel(_petListIndex,_parentGO,_petId,_genes)
    end
    --母亲
    local _motherInfo = currentPetInfo.geneticMap[PetManager.FamilyTree.Mother]
    if table.getn(_motherInfo.genes) > 0 then
        --self.Txt_MotherName.text = _motherInfo.petName
        --self.Img_MotherIcon.url = _motherInfo.petIcon
        local _petMutationLevel = PetManager.GetMutationLevel(_motherInfo.genes)
        self.Btn_Mother:GetController("level"):SetSelectedIndex(_petMutationLevel-1)
        self.Btn_Mother:GetChild("frame").url = PetManager.GetMutationLevelFrameIcon(_petMutationLevel)
        --显示母亲宠物模型
        _petListIndex = PetManager.FamilyTree.Mother + 1
        _parentGO = self.Img_MotherIcon
        _petId = _motherInfo.petId
        _genes = _motherInfo.genes
        this.showFamilyTreePetModel(_petListIndex,_parentGO,_petId,_genes)
    end

    --爷爷
    local _grandfatherInfo = currentPetInfo.geneticMap[PetManager.FamilyTree.Grandfather]
    local _hasGrandfather = table.getn(_grandfatherInfo.genes) > 0
    if _hasGrandfather then
        --self.Txt_GrandfatherName.text = _grandfatherInfo.petName
        --self.Img_GrandfatherIcon.url = _grandfatherInfo.petIcon
        local _petMutationLevel = PetManager.GetMutationLevel(_grandfatherInfo.genes)
        self.Btn_Grandfather:GetController("level"):SetSelectedIndex(_petMutationLevel-1)
        self.Btn_Grandfather:GetChild("frame").url = PetManager.GetMutationLevelFrameIcon(_petMutationLevel)
        --显示爷爷宠物模型
        _petListIndex = PetManager.FamilyTree.Grandfather + 1
        _parentGO = self.Img_GrandfatherIcon
        _petId = _grandfatherInfo.petId
        _genes = _grandfatherInfo.genes
        this.showFamilyTreePetModel(_petListIndex,_parentGO,_petId,_genes)
    end
    --奶奶
    local _grandmotherInfo = currentPetInfo.geneticMap[PetManager.FamilyTree.Grandmother]
    if table.getn(_grandmotherInfo.genes) > 0  then
        --self.Txt_GrandmotherName.text = _grandmotherInfo.petName
        --self.Img_GrandmotherIcon.url = _grandmotherInfo.petIcon
        local _petMutationLevel = PetManager.GetMutationLevel(_grandmotherInfo.genes)
        self.Btn_Grandmother:GetController("level"):SetSelectedIndex(_petMutationLevel-1)
        self.Btn_Grandmother:GetChild("frame").url = PetManager.GetMutationLevelFrameIcon(_petMutationLevel)
        --显示奶奶宠物模型
        _petListIndex = PetManager.FamilyTree.Grandmother + 1
        _parentGO = self.Img_GrandmotherIcon
        _petId = _grandmotherInfo.petId
        _genes = _grandmotherInfo.genes
        this.showFamilyTreePetModel(_petListIndex,_parentGO,_petId,_genes)
    end
    --外公
    local _grandpaInfo = currentPetInfo.geneticMap[PetManager.FamilyTree.Grandpa]
    local _hasGrandpa = table.getn(_grandpaInfo.genes) > 0
    if _hasGrandpa then
        --self.Txt_GrandpaName.text = _grandpaInfo.petName
        --self.Img_GrandpaIcon.url = _grandpaInfo.petIcon
        local _petMutationLevel = PetManager.GetMutationLevel(_grandpaInfo.genes)
        self.Btn_Grandpa:GetController("level"):SetSelectedIndex(_petMutationLevel-1)
        self.Btn_Grandpa:GetChild("frame").url = PetManager.GetMutationLevelFrameIcon(_petMutationLevel)
        --显示外公宠物模型
        _petListIndex = PetManager.FamilyTree.Grandpa + 1
        _parentGO = self.Img_GrandpaIcon
        _petId = _grandpaInfo.petId
        _genes = _grandpaInfo.genes
        this.showFamilyTreePetModel(_petListIndex,_parentGO,_petId,_genes)
    end
    --外婆
    local _grandmaInfo = currentPetInfo.geneticMap[PetManager.FamilyTree.Grandma]
    if table.getn(_grandmaInfo.genes) > 0  then
        --self.Txt_GrandmaName.text = _grandmaInfo.petName
        --self.Img_GrandmaIcon.url = _grandmaInfo.petIcon
        local _petMutationLevel = PetManager.GetMutationLevel(_grandmaInfo.genes)
        self.Btn_Grandma:GetController("level"):SetSelectedIndex(_petMutationLevel-1)
        self.Btn_Grandma:GetChild("frame").url = PetManager.GetMutationLevelFrameIcon(_petMutationLevel)
        --显示外婆宠物模型
        _petListIndex = PetManager.FamilyTree.Grandma + 1
        _parentGO = self.Img_GrandmaIcon
        _petId = _grandmaInfo.petId
        _genes = _grandmaInfo.genes
        this.showFamilyTreePetModel(_petListIndex,_parentGO,_petId,_genes)
    end

    local _selectedIndex = GenerationType.Self
    if _hasFather then
        if _hasGrandfather then
            if _hasGrandpa then
                _selectedIndex = GenerationType.Grandchildren
            else
                _selectedIndex = GenerationType.NoGrandpa
            end
        elseif _hasGrandpa then
            _selectedIndex = GenerationType.NoGrandFather
        else
            _selectedIndex = GenerationType.Children
        end
    end
    self.GenerationController:SetSelectedIndex(_selectedIndex)
end

function this:showPartList()
    partListIndex = 0
    partListSelected = 1
    for i = 1, #currentPetInfo.genes do

        this:rendererPartListItem(self.List_Part[i])
    end
    --self.List_Part.numItems = #currentPetInfo.genes
end
function this:showPetInfo()
    local _familyMemberInfo = this:getFamilyMemberInfo()

    self.Txt_PetName.text = _familyMemberInfo.petName
    self.Txt_OwnerName.text = _familyMemberInfo.playerName
    this:showSelectedPetModel()
end

function this:rendererPartListItem(obj)
    partListIndex= partListIndex+1
    local _familyMemberInfo = this:getFamilyMemberInfo()
    local _geneId = _familyMemberInfo.genes[partListIndex]
    local ctrlItem = CtrlManager.GetCtrl(PanelNames.PetPartItem);
    ctrlItem.initData(obj, _geneId);

    --local _geneItem = G.dataTable["t_elvesgene"][tostring(_geneId)]
    --local _partName = getLanguage(_geneItem.f_GeneName)
    --obj:GetChild("title").text = _partName
    --obj.selected = partListSelected == partListIndex
    --
    --local _petMutationLevel = _geneItem.f_MutationLevel
    --obj:GetController("level"):SetSelectedIndex(_petMutationLevel-1)
end

function this:getFamilyMemberInfo()
    local _familyMemberInfo = nil
    if selectedFamilyMember == PetManager.FamilyTree.MySelf then
        _familyMemberInfo = currentPetInfo
    elseif selectedFamilyMember == PetManager.FamilyTree.Father then
        _familyMemberInfo = currentPetInfo.geneticMap[PetManager.FamilyTree.Father]
    elseif selectedFamilyMember == PetManager.FamilyTree.Mother then
        _familyMemberInfo = currentPetInfo.geneticMap[PetManager.FamilyTree.Mother]
    elseif selectedFamilyMember == PetManager.FamilyTree.Grandfather then
        _familyMemberInfo = currentPetInfo.geneticMap[PetManager.FamilyTree.Grandfather]
    elseif selectedFamilyMember == PetManager.FamilyTree.Grandmother then
        _familyMemberInfo = currentPetInfo.geneticMap[PetManager.FamilyTree.Grandmother]
    elseif selectedFamilyMember == PetManager.FamilyTree.Grandpa then
        _familyMemberInfo = currentPetInfo.geneticMap[PetManager.FamilyTree.Grandpa]
    elseif selectedFamilyMember == PetManager.FamilyTree.Grandma then
        _familyMemberInfo = currentPetInfo.geneticMap[PetManager.FamilyTree.Grandma]
    end
    return _familyMemberInfo
end

--点击
function this:checkButtonSelected()
    self.Btn_MySelf.selected = selectedFamilyMember == PetManager.FamilyTree.MySelf
    self.Btn_Father.selected = selectedFamilyMember == PetManager.FamilyTree.Father
    self.Btn_Mother.selected = selectedFamilyMember == PetManager.FamilyTree.Mother
    self.Btn_Grandfather.selected = selectedFamilyMember == PetManager.FamilyTree.Grandfather
    self.Btn_Grandmother.selected = selectedFamilyMember == PetManager.FamilyTree.Grandmother
    self.Btn_Grandpa.selected = selectedFamilyMember == PetManager.FamilyTree.Grandpa
    self.Btn_Grandma.selected = selectedFamilyMember == PetManager.FamilyTree.Grandma
end
function this:onClickChild()
    this:refreshPetInfo(PetManager.FamilyTree.MySelf)
end
function this:onClickFather()
    this:refreshPetInfo(PetManager.FamilyTree.Father)
end
function this:onClickMother()
    this:refreshPetInfo(PetManager.FamilyTree.Mother)
end
function this:onClickGrandfather()
    this:refreshPetInfo(PetManager.FamilyTree.Grandfather)
end
function this:onClickGrandmother()
    this:refreshPetInfo(PetManager.FamilyTree.Grandmother)
end
function this:onClickGrandpa()
    this:refreshPetInfo(PetManager.FamilyTree.Grandpa)
end
function this:onClickGrandma()
    this:refreshPetInfo(PetManager.FamilyTree.Grandma)
end

--显示宠物模型
function this:removePetModel()
    if self.PetModelGO then
        destroy(self.PetModelGO)
    end
    self.PetModelGO = nil
end
function this:showSelectedPetModel()
    local _petInfo = this:getFamilyMemberInfo()
    if _petInfo and #_petInfo.genes == PetManager.PetPart.Ornament then
        local _petModelInfo = {}
        _petModelInfo.parentGO = self.Model_Pet
        _petModelInfo.petId = _petInfo.petId
        if self.PetModelGO and self.goWrapper then
            _petModelInfo.modelGO = self.PetModelGO
            _petModelInfo.modelWrapper = self.goWrapper
            PetManager.PetModelRefresh(_petModelInfo,_petInfo.genes,true)
        else
            this:removePetModel()
            resMgr:LoadModelPrefab(PetManager.PetModelPrefab,function(go)
                self.PetModelGO = newObject(go)

                local com = this.PetModelGO:GetComponent("SkeletonGraphic")

                if this.PetModelGO and com then
                    com.material = resMgr.Pet_Copy_Mat
                    com.material:SetFloat("_StencilComp",8)
                    com.material:SetFloat("_Stencil",1);
                end
                
                local _modelWrapper = self.goWrapper
                self.goWrapper = GoWrapper.New()
                _petModelInfo.modelGO = self.PetModelGO
                _petModelInfo.modelWrapper = self.goWrapper
                PetManager.PetModelFirstSet(_petModelInfo,_petInfo.genes,true,Vector3.New(0,0,0),Vector3.New(1.2,1.2,1))
                if _modelWrapper then
                    _modelWrapper:Dispose()
                end
            end)
        end
    end
end

function this:initModelTable()
    if this.petListModelTable == nil then
        this.petListModelTable = {}
    end
end
function this:removeModelTable()
    if this.petListModelTable then
        for index, _ in pairs(this.petListModelTable) do
            this.removePetListModelByIndex(index)
        end
    end
end
function this.removePetListModelByIndex(index)
    if this.petListModelTable[index].modelGO then
        destroyImmediate(this.petListModelTable[index].modelGO)
    end
    this.petListModelTable[index].modelGO = nil
    this.petListModelTable[index].parentGO = nil
    this.petListModelTable[index].petId = 0

end
function this.showFamilyTreePetModel(petListIndex,parentGO,petId,genes)
    --显示宠物模型
    if this.petListModelTable[petListIndex] == nil then
        this.petListModelTable[petListIndex] = {}
    end
    if this.petListModelTable[petListIndex].modelGO and this.petListModelTable[petListIndex].petId == petId then
        PetManager.PetModelRefresh(this.petListModelTable[petListIndex],genes,false)
    else
        this.removePetListModelByIndex(petListIndex)
        resMgr:LoadModelPrefab(PetManager.PetModelPrefab,function(go)
            this.petListModelTable[petListIndex].modelGO = newObject(go)

            local com = this.petListModelTable[petListIndex].modelGO:GetComponent("SkeletonGraphic")

             if this.petListModelTable[petListIndex].modelGO and com then
                 com.material = resMgr.Pet_Copy_Mat
                 com.material:SetFloat("_StencilComp",8)
                 com.material:SetFloat("_Stencil",1);
             end

            this.petListModelTable[petListIndex].parentGO = parentGO
            local _modelWrapper = this.petListModelTable[petListIndex].modelWrapper
            this.petListModelTable[petListIndex].modelWrapper = GoWrapper.New()
            this.petListModelTable[petListIndex].petId = petId
            PetManager.PetModelFirstSet(this.petListModelTable[petListIndex],genes,false,Vector3.New(0,0,0),Vector3.New(0.55,0.55,1))
            if _modelWrapper then
                _modelWrapper:Dispose()
            end
        end)
    end
end

return this